package ntd.util.frame.action;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Page;
import cn.hutool.db.sql.Order;
import cn.hutool.json.JSONUtil;
import ntd.action.bo.ActionInBo;

public class ActionPage {

    public static Page page(ActionInBo actionInBo) {
        return new Page(getPage(actionInBo), getSize(actionInBo));
    }

    public static Page page(ActionInBo actionInBo, Order order) {
        return new Page(getPage(actionInBo), getSize(actionInBo), order);
    }

    public static int getPage(ActionInBo actionInBo) {
        String page = actionInBo.getParamStr("page");
        if (JSONUtil.isTypeJSON(page)) {
            page = JSONUtil.parseObj(page).getStr("page");
        }
        if (StrUtil.isBlank(page) || !NumberUtil.isNumber(page)) {
            return 0;
        }
        return NumberUtil.parseNumber(page).intValue();
    }

    public static int getSize(ActionInBo actionInBo) {
        String size = actionInBo.getParamStr("size");
        if (StrUtil.isBlank(size)) {
            String page = actionInBo.getParamStr("page");
            if (JSONUtil.isTypeJSON(page)) {
                size = JSONUtil.parseObj(page).getStr("limit");
            }
        }
        if (StrUtil.isBlank(size) || !NumberUtil.isNumber(size)) {
            return 18;
        }
        return NumberUtil.parseNumber(size).intValue();
    }
}
